summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/psc/time/clocks/system_clock_core.cpp
blob: c507ef51724277e878364b6f1f46a7de25a46ab6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "core/hle/service/psc/time/clocks/context_writers.h"
#include "core/hle/service/psc/time/clocks/system_clock_core.h"

namespace Service::PSC::Time {

bool SystemClockCore::CheckClockSourceMatches() {
    SystemClockContext context{};
    if (GetContext(context) != ResultSuccess) {
        return false;
    }

    SteadyClockTimePoint time_point{};
    if (m_steady_clock.GetCurrentTimePoint(time_point) != ResultSuccess) {
        return false;
    }

    return context.steady_time_point.IdMatches(time_point);
}

Result SystemClockCore::GetCurrentTime(s64* out_time) const {
    R_UNLESS(out_time != nullptr, ResultInvalidArgument);

    SystemClockContext context{};
    SteadyClockTimePoint time_point{};

    R_TRY(m_steady_clock.GetCurrentTimePoint(time_point));
    R_TRY(GetContext(context));

    R_UNLESS(context.steady_time_point.IdMatches(time_point), ResultClockMismatch);

    *out_time = context.offset + time_point.time_point;
    R_SUCCEED();
}

Result SystemClockCore::SetCurrentTime(s64 time) {
    SteadyClockTimePoint time_point{};
    R_TRY(m_steady_clock.GetCurrentTimePoint(time_point));

    SystemClockContext context{
        .offset = time - time_point.time_point,
        .steady_time_point = time_point,
    };
    R_RETURN(SetContextAndWrite(context));
}

Result SystemClockCore::GetContext(SystemClockContext& out_context) const {
    out_context = m_context;
    R_SUCCEED();
}

Result SystemClockCore::SetContext(SystemClockContext& context) {
    m_context = context;
    R_SUCCEED();
}

Result SystemClockCore::SetContextAndWrite(SystemClockContext& context) {
    R_TRY(SetContext(context));

    if (m_context_writer) {
        R_RETURN(m_context_writer->Write(context));
    }

    R_SUCCEED();
}

void SystemClockCore::LinkOperationEvent(OperationEvent& operation_event) {
    if (m_context_writer) {
        m_context_writer->Link(operation_event);
    }
}

} // namespace Service::PSC::Time